package com.wlb365.financing.modules.template.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.wlb365.financing.common.constants.ApiConstants;
import com.wlb365.financing.common.utils.ShiroUtil;
import com.wlb365.financing.modules.template.request.SubscribeDataReq;
import com.wlb365.financing.modules.template.request.TemplateQueryByTypeReq;
import com.wlb365.financing.modules.template.request.TemplateQueryPageReq;
import com.wlb365.financing.modules.template.request.TemplateReq;
import com.wlb365.financing.modules.template.response.TemplatePageRes;
import com.wlb365.financing.modules.template.response.TemplateRes;
import com.wlb365.financing.modules.template.service.ITemplateService;
import com.wlb365.sdk.client.SdkClient;
import com.wlb365.sdk.model.CommonRequest;
import com.wlb365.sdk.model.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author LuHang
 * @Date 2021/5/18 10:13
 * @Version 1.0
 */
@Service
public class TemplateServiceImpl  implements ITemplateService {

    @Autowired
    private SdkClient sdkClient;

    @Override
    public TemplatePageRes queryTemplatePage(TemplateQueryPageReq templateQueryPageReq) {
        CommonRequest commonRequest = new CommonRequest(ApiConstants.DATA_CENTER_TEMPLATE_PAGE);
        Map<String, Object> params = Maps.newHashMap();
        params.put("current", templateQueryPageReq.getCurrent());
        params.put("size", templateQueryPageReq.getSize());
        params.put("name", templateQueryPageReq.getName());
        params.put("templateType", templateQueryPageReq.getTemplateType());
        params.put("templateBtype", templateQueryPageReq.getTemplateBtype());
        params.put("subscribeBy", ShiroUtil.getUser().getDeptId());
        commonRequest.setBizModel(params);
        CommonResponse commonResponse =  sdkClient.execute(commonRequest);
        TemplatePageRes templatePageRes = JSONUtil.toBean(commonResponse.getData(), TemplatePageRes.class);
        return templatePageRes;
    }

    @Override
    public TemplateRes addTemplate(TemplateReq templateReq) {
        CommonRequest commonRequest = new CommonRequest(ApiConstants.DATA_CENTER_TEMPLATE_ADD);
        Map<String, Object> params = Maps.newHashMap();
        params.put("name", templateReq.getName());
        params.put("description", templateReq.getDescription());
        params.put("templateType", templateReq.getTemplateType());
        params.put("templateBtype", templateReq.getTemplateBtype());
        params.put("templateData", templateReq.getTemplateData());
        commonRequest.setBizModel(params);
        CommonResponse commonResponse =  sdkClient.execute(commonRequest);
        TemplateRes templatePageRes = JSONUtil.toBean(commonResponse.getData(), TemplateRes.class);
        return templatePageRes;
    }

    @Override
    public void addSubscribe(SubscribeDataReq subscribeDataReq) {
        CommonRequest commonRequest = new CommonRequest(ApiConstants.DATA_CENTER_SUBCRIBE_ADD);
        Map<String, Object> params = Maps.newHashMap();
        params.put("templateId", subscribeDataReq.getTemplateId());
        params.put("subscribeBy", subscribeDataReq.getSubscribeBy());
        commonRequest.setBizModel(params);
        sdkClient.execute(commonRequest);
    }

    @Override
    public TemplateRes selectOneByType(TemplateQueryByTypeReq templateQueryByTypeReq) {
        CommonRequest commonRequest = new CommonRequest(ApiConstants.DATA_CENTER_TEMPLATE_SELECTONEBYTYPE);
        Map<String, Object> params = Maps.newHashMap();
        params.put("templateBtype", templateQueryByTypeReq.getTemplateBtype());
        params.put("subscribeBy", ShiroUtil.getUser().getDeptId());
        commonRequest.setBizModel(params);
        CommonResponse commonResponse =  sdkClient.execute(commonRequest);
        TemplateRes templateRes = JSONUtil.toBean(commonResponse.getData(),TemplateRes.class);
        return templateRes;
    }

    @Override
    public TemplateRes selectOne(String id) {
        CommonRequest commonRequest = new CommonRequest(ApiConstants.DATA_CENTER_TEMPLATE_ONE);
        Map<String, Object> params = Maps.newHashMap();
        params.put("id", id);
        commonRequest.setBizModel(params);
        CommonResponse commonResponse =  sdkClient.execute(commonRequest);
        TemplateRes templateRes = JSONUtil.toBean(commonResponse.getData(),TemplateRes.class);
        return templateRes;
    }

    @Override
    public TemplateRes update(TemplateReq templateReq) {
        CommonRequest commonRequest = new CommonRequest(ApiConstants.DATA_CENTER_TEMPLATE_UPDATE);
        Map<String, Object> params = Maps.newHashMap();
        params.put("id", templateReq.getId());
        params.put("name", templateReq.getName());
        params.put("description", templateReq.getDescription());
        params.put("templateType", templateReq.getTemplateType());
        params.put("templateBtype", templateReq.getTemplateBtype());
        params.put("templateData", templateReq.getTemplateData());
        commonRequest.setBizModel(params);
        CommonResponse commonResponse =  sdkClient.execute(commonRequest);
        TemplateRes templateRes = JSONUtil.toBean(commonResponse.getData(),TemplateRes.class);
        return templateRes;
    }

    @Override
    public boolean del(List<String> ids) {
        CommonRequest commonRequest = new CommonRequest(ApiConstants.DATA_CENTER_TEMPLATE_DEL);
        Map<String, Object> params = Maps.newHashMap();
        params.put("id", ids);
        commonRequest.setBizModel(params);
        CommonResponse commonResponse =  sdkClient.execute(commonRequest);
        return true;
    }
}
